package com.jeeplus.modules.work.queue.web;

import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.modules.work.queue.entity.QuquEntity;
import com.jeeplus.modules.work.queue.service.QueueService;
import okhttp3.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import net.sf.json.JSONObject;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;



@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/queue")
public class QueueController {

    /**
     * 窗口人员变更通知接口
     */
    @Value("${AlterationInformURL}")
    private String AlterationInformURL;
    /**
     * 排队叫号接口
     */
    @Value("${QueuingURL}")
    private String QueuingURL;


    private final QueueService service;
    private static final String url="http://demo4.roymark.com.cn:800/RoyMark_Queue_CdGxq/queueReport/lineReportInterface.action";


    @PostMapping("findQueueNow")
//    @ApiOperation(value = "查询当天排队叫号信息")
    boolean findQueueNow(String areaId,String status ,String number){
        if(null!=areaId && !"".equals(areaId)&&null!=status && !"".equals(status)&&null!=number && !"".equals(number)){
            Map<String, Object> map = new HashMap<>();
            map.put("areaId",areaId);
            map.put("number",number);
            QuquEntity queueNow = service.findQueueNow(map);
            //传过来的status有0和1分别对应状态取号和受理
            if("0".equals(status)){
                queueNow.setLineState("1");
            }else if("1".equals(status)){
                queueNow.setLineState("3");
            }
            JSONObject jsonObject = JSONObject.fromObject(queueNow);
            try {
                OkHttpClient client = new OkHttpClient();
                MediaType mediaType = MediaType.parse("application/json");
                RequestBody body = RequestBody.create(mediaType,jsonObject.toString());
                Request request = new Request.Builder()
                        .url(url)
                        .post(body)
                        .addHeader("Content-Type", "application/json")
                        .addHeader("cache-control", "no-cache")
                        .addHeader("Postman-Token", "71c5dd87-dbe0-43fc-aae3-7e32f9a61b61")
                        .build();

                Response response = client.newCall(request).execute();
                System.out.println(response.body().string().toString());
            } catch (ClientProtocolException e) {
                System.err.println("Http协议出现问题");
                e.printStackTrace();
            } catch (ParseException e) {
                System.err.println("解析错误");
                e.printStackTrace();
            } catch (IOException e) {
                System.err.println("IO异常");
                e.printStackTrace();
            }
            return true;

        }
        return false;

    }
    /**
     * 对接—排队叫号信息_开始叫号
     * @param number 叫号编号
     * @return 排队叫号信息
     */
    @GetMapping("queuingInfoStart")
    public String queuingInfoStart(String number,String sysno,String winNo,String userNo){
        String windowId = service.getWindowId(sysno, winNo);
        String staffId = service.getStaffId(sysno, userNo);
        Map<String, Object> map = service.queuingInfoStart(number,sysno);
        String url = QueuingURL+"?type=1" +
                "&id="+map.get("id") +
                "&business="+ map.get("business")+
                "&flownum="+map.get("flownum") +
                "&name=" +map.get("name")+
                "&mobile="+ map.get("mobile")+
                "&idcard="+ map.get("idcard")+
                "&window="+ windowId+
                "&workman="+ staffId+
                "&style=2" +
                "&taketime="+ map.get("taketime")+
                "&calltime="+map.get("calltime");
        String s = webService(url);
        return s;
    }
    /**
     * 对接—排队叫号信息_结束
     * @param number 叫号编号
     * @return 排队叫号信息
     */
    @GetMapping("queuingInfoEnd")
    public String queuingInfoEnd(String number,String sysno){
        Map<String, Object> map = service.queuingInfoStart(number,sysno);
        //调用接口
        String url = QueuingURL + "?type=2" +
                "&id=" +map.get("id")+
                "&endtime="+map.get("endtime");
        String s = webService(url);
        return s;
    }

    /**
     * 对接—窗口
     * @return 窗口集合
     */
    @GetMapping("window")
   public AjaxJson window(){
        return AjaxJson.success().put("data",service.window());
    }
    /**
     * 对接—人员
     * @return 人员集合
     */
    @GetMapping("staff")
    public AjaxJson staff(){
        return AjaxJson.success().put("data",service.staff());
    }
//    /**
//     * 写成定时
//     * 对接—人员窗口变动通知
//     * @return
//     */
//    @GetMapping("changeInform")
//    @ApiOperation(value = "排队叫号对接—人员窗口变动通知")
//    public String changeInform(){
//        return webService(AlterationInformURL);
//    }


    /**
     * 对接—窗口查询当前办理
     * @param windowId 窗口ID
     * @return 窗口当前办理信息
     */
    @GetMapping("findHandleInfoByWindow")
    public AjaxJson findHandleInfoByWindow(String windowId,String sysno){
        return AjaxJson.success().put("data",service.findHandleInfoByWindow(windowId));
    }


    /**
     * 对接—区域
     * @return 全部区划
     */
    @GetMapping("getArea")
    public AjaxJson getArea(){
        return AjaxJson.success().put("data",service.getArea());
    }

    /**
     * okhttp调用接口方法
     * @param url 路径
     * @return 其他接口返回的数据
     */
    public String webService(String url){
        String string ="";
        try {
        OkHttpClient client = new OkHttpClient().newBuilder()
                .build();
        Request request = new Request.Builder()
                .url(url)
                .method("GET", null)
                .build();
        Response response = client.newCall(request).execute();
             string = response.body().string().toString();
        } catch (ClientProtocolException e) {
            System.err.println("Http协议出现问题");
            e.printStackTrace();
        } catch (ParseException e) {
            System.err.println("解析错误");
            e.printStackTrace();
        } catch (IOException e) {
            System.err.println("IO异常");
            e.printStackTrace();
        }
        return string;
    }
//    http://171.221.172.122:6868/api/queue/staff

    //张哥需要排队叫号接口文档发给程老师
    @PostMapping(value ="findStreetCode")
    public AjaxJson findStreetCode() {

        return AjaxJson.success().put("data",service.findStreetCode());
    }

    @PostMapping(value ="findQueueInfo")
    public AjaxJson findQueueInfo(String areaId) {

        return AjaxJson.success().put("data",service.findQueueInfo(areaId));
    }

}
